﻿using AnesSystem.Controls;
using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Udp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using AnesSystem;
using AnesSystem.Model;

namespace AnesSystem.ViewModels
{
    public class UrgentOperDialogWinViewModel : BaseViewModel<UrgentOperDialogWinViewModel>
    {

        #region Variables
        private string _zyh;
        private DispatcherTimer timer;
        private int i = 0;
        private UrgentOperDialogWin _urgentWin;
        #endregion

        #region Constructor

        public UrgentOperDialogWinViewModel()
        {
            InitalTimer();
        }

        #endregion

        #region Properties
        public string Zyh
        {
            get
            {
                return _zyh;
            }

            set
            {
                _zyh = value;
                RaisePropertyChanged("Zyh");
            }
        }

        public UrgentOperDialogWin UrgentWin
        {
            get
            {
                return _urgentWin;
            }

            set
            {
                _urgentWin = value;
            }
        }


        #endregion

        #region 依赖属性

        #endregion

        #region Event

        public event EventHandler SerchZyhSuccessed;

        #endregion

        #region Private Methods
        private void InitalTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += Timer_Tick;
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (i < 20)
            {
                List<PatientLinkModel> patientLinkList = PatientLinkService.Instance.GetPatientByZyh(Zyh);
                if (patientLinkList != null && patientLinkList.Count > 0)
                {
                    PatientLinkModel patientLink = patientLinkList.Find(delegate (PatientLinkModel p) { return p.Application.Status.Equals("麻醉术前"); });//只返回第一个匹配
                    if (patientLink!=null) {
                        UrgentPatient(patientLink);
                        MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                        i = 0;
                        timer.Stop();
                        return;
                    }                  
                }
                i++;
            }
            else
            {
                MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                i = 0;
                Zyh = "";
                timer.Stop();
                MessageBoxCommon.ShowDialog("未查到此人新住院信息！！！", MessageBoxCommon.OK);
            }
        }

        #endregion

        #region Public/Protected Methods
        public void GetPatient()
        {
            Task.Factory.StartNew(delegate
            {
                List<PatientLinkModel> patientLinkList = PatientLinkService.Instance.GetPatientByZyh(Zyh);
                if (patientLinkList == null || patientLinkList.Count < 1)
                {
                    UdpModel udm = new UdpModel();
                    udm.Zyh = Zyh;
                    UdpMangager.Instance.SendUdpServerMsg(ServiceSign.PATIENT_ADD,udm);
                    timer.Start();//执行查询方法的线程 
                }
                else
                {
                    PatientLinkModel patientLink = patientLinkList.Find(delegate (PatientLinkModel p) { return p.Application.Status.Equals("麻醉术前"); });//只返回第一个匹配
                    if (patientLink != null)
                    {
                        MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                        UrgentPatient(patientLink);
                        return;
                    }
                    else
                    {
                        UdpModel udm = new UdpModel();
                        udm.Zyh = Zyh;
                        UdpMangager.Instance.SendUdpServerMsg(ServiceSign.PATIENT_ADD, udm);
                        timer.Start();//执行查询方法的线程 
                    }
                }
            });
            MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Visible;
        }

        public void Excute(string commond)
        {
            switch (commond)
            {
                case "Confirm":
                    {
                        if (Zyh == null || "".Equals(Zyh))
                        {
                            MessageBoxCommon.ShowDialog("请输入住院号重试！！！", MessageBoxCommon.OK);
                        }
                        else
                        {
                            UrgentWin.CloseWin(true);
                            GetPatient();
                        }
                    }
                    break;
                case "Skip":
                    {
                        PatientLinkModel patientLink = PatientLinkModel.CtreatObject();
                        patientLink.Patient.Zycs = "1";
                        UrgentPatient(patientLink);
                        UrgentWin.CloseWin(true);
                    }
                    break;
            }
        }

        public void UrgentPatient(PatientLinkModel patientLink)
        {
            patientLink.CreateDate = DateTime.Now;
            patientLink.Schedule.Pbssjh = DataManager.Instance.CurRoom.Name;
            patientLink.Schedule.Jhsssj = DateTime.Now;
            patientLink.Patient.Status = "valid";
            patientLink.Application.Type = Constants.OR_TYPE_URGENT;
            patientLink.Application.Detailstatus = Constants.OR_STATUS_FLAG_SCHEDULED;
            patientLink.Application.Status = Constants.AP_STATUS_MAZUIQIAN;
            patientLink.Schedule.PbtsInt = 1;
            SecheduleMainCtrlViewModel.Instance.PaiTai(patientLink, DataManager.Instance.CurRoom.Name);
            PatientLinkService.Instance.AddUpdate(patientLink);
            DataManager.Instance.CurrentPatientLink = patientLink;
            Zyh = "";
            SerchZyhSuccessed?.Invoke(this, null);
        }

        #endregion

        #region EventHandlers

        #endregion

    }
}
